local map = require "modules.map"
local logger = require "utils.logger"
local movement = require "modules.movement"
local combat = require "modules.combat"
local tslib = require "TSLib"

-- 设置挂机界面的技能
local function setSkill(skill_button)
    combat.stopAutoAttack()
    --  当前是技能页面，需要切换到设置界面
    if multiColor({{1548, 825, 0x886037},{1541, 813, 0xaaa3a0},{1578, 816, 0x84623d},{1555, 834, 0x323b3e}}) then
        tap(1549,410)
        mSleep(500)
    end

    -- 挂机页面点击设置
    local guaji_buttons = {{1460, 617, 0xb6b6b6},{1356, 636, 0xb89a67},{544, 295, 0x101010}}
    movement.clickSeries(guaji_buttons)
    -- 添加新增技能按钮坐标
    local add_skill_buttons = {{1257, 235, 0x080603},{1322, 703, 0xffffff},{1078, 711, 0x8d8171},skill_button,{670, 172, 0x681f0f},{1441, 169, 0x63431b},{1356, 123, 0x9d6a2b}}
    movement.clickSeries(add_skill_buttons)
end

-- 异常点击处理，尝试点击关闭界面按钮
local function handleExceptionClick()
    logger.error("脚本异常，正在重启")
    movement.clickSeries({{670, 172, 0x681f0f},{1441, 169, 0x63431b},{1356, 123, 0x9d6a2b}})
end

-- 设置诱惑之光
local function setyouhuo()
    setSkill({604, 658, 0xebb65a})
end

-- 设置雷电术
local function setLightning()
    setSkill({604, 585, 0xebb65a})
end

local function main()
    if multiColor({{748, 12, 0x98e5e8},{750, 16, 0x32d5db},{752, 20, 0x08b1b8},{757, 20, 0x59d9de}}) then
        tap(748, 12)
    end
    -- 是否打开客服，关闭客服
    if multiColor({{809, 27, 0xffe11f},{809, 39, 0xffe11f},{809, 44, 0xffe11f},{807, 54, 0xfee01f}}) then
        tap(44, 42)
    end
    logger.info("运行召唤宝宝的脚本")
    if multiColor({ { 1274, 410, 0xdbce96 }, { 1279, 410, 0xcdba7a }, { 1288, 410, 0xd1c27d }, { 1288, 417, 0xb09454 } }) then
        logger.info("检测到宝宝，开始执行任务")
    else
        logger.info("未检测到宝宝，开始执行任务")
        combat.stopAutoAttack()
        movement.useTownStone()
        mSleep(800)
        setyouhuo()
        if map.enterMapByPoint("封魔谷",1) then
            combat.startAutoAttack()
            mSleep(300 * 1000)
            combat.stopAutoAttack()
            movement.useTownStone()
            mSleep(800)
            setLightning()
            return true
        else
            logger.error("进入地图失败")
            return false
        end
    end
end

return {
    run = main,
    lightning = setLightning,
    youhuo = setyouhuo
}
